package cn.team11.sds.dayi.envconfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.aliyun.alink.bone.CdnEnv;
import com.aliyun.alink.business.alink.ALinkEnv;

import cn.team11.sdsPackager.T11PackageConfig;
import cn.team11.util.T11Log;

/**
 * Created by huanyu.zhy on 17/6/6.
 * <p>
 * 当前环境配置
 */
public class EnvConfigure {
    public static ALinkEnv aLinkEnv = null;
    public static CdnEnv cdnEnv = null;

    static public void init(Context context) {
        if (!T11PackageConfig.useT11DefaultEnv) {
            try {
                SharedPreferences sp = context.getSharedPreferences("envConfig", 0);
                if (sp != null) {
                    String alinkEnvConf = sp.getString("aLinkEnv", (String) null);
                    if (!TextUtils.isEmpty(alinkEnvConf))
                        aLinkEnv = ALinkEnv.valueOf(alinkEnvConf);

                    String cdnEnvConf = sp.getString("cdnEnv", (String) null);
                    if (!TextUtils.isEmpty(cdnEnvConf))
                        cdnEnv = CdnEnv.valueOf(cdnEnvConf);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            aLinkEnv = T11PackageConfig.aLinkEnv;
            cdnEnv = T11PackageConfig.cdnEnv;
        }
        T11Log.d("==============设置当前的env==============");
        T11Log.d("==============设置当前的env alinkEnv = " + aLinkEnv + "==============");
        T11Log.d("==============设置当前的env cdnEnv = " + cdnEnv + "==============");
    }


    static public void saveAlinkEnv(Context context, ALinkEnv env) {
        aLinkEnv = env;
        SharedPreferences sharedPreferences = context.getSharedPreferences("envConfig", 0);
        Editor editor = sharedPreferences.edit();
        editor.putString("aLinkEnv", env.name()).commit();
    }

    static public void saveCDNEnv(Context context, CdnEnv env) {
        cdnEnv = env;
        SharedPreferences sharedPreferences = context.getSharedPreferences("envConfig", 0);
        Editor editor = sharedPreferences.edit();
        editor.putString("cdnEnv", env.name()).commit();
    }
}
